package com.iotteam.androidbase.moudules.main.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseFragment;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.main.presenter.ISensorListPresenter;
import com.iotteam.androidbase.moudules.main.presenter.SensorListCompl;

/**
 * Created by lidechen on 5/2/17.
 */

public class SensorListFragment extends BaseFragment implements ISensorListView {

    protected View mRootView;
    private Button mTestBtn;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private ISensorListPresenter mPresenter = new SensorListCompl(SensorListFragment.this);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_sensor_list, container, false);
        }
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swiprefresh);

        //mSwipeRefreshLayout.setOnChildScrollUpCallback();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                load();
            }
        });

        mTestBtn = (Button) mRootView.findViewById(R.id.btn_test);

        mTestBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "hello", Toast.LENGTH_LONG).show();
            }
        });

        mIsCreateView = true;
        return mRootView;
    }

    private void load(){
        if(!LocalInfoManager.getInstance().isLogin()){
            ToastUtil.showShort(R.string.dialog_login_request);
            endRefreshView();
        }else{
            mPresenter.loadSensorList();
        }
    }

    @Override
    protected void lazyLoad() {
        load();
    }

    @Override
    public void startRefreshView() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void endRefreshView() {
        mSwipeRefreshLayout.setRefreshing(false);
    }
}
